package coop.base.app;

import java.util.List;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import coop.base.dao.PersonDao;
import coop.base.model.Person;

/**
 * App class with main method to test our DAO
 * 
 * @author DevCrumb.com
 */
@Component
public class App {

	
    public static void main(String[] args) {
    	ClassPathXmlApplicationContext context = new  
    			  ClassPathXmlApplicationContext("applicationContext.xml");
    	
    	
    	    	
//    	PersonDaoImpl dao = (PersonDaoImpl) context.getBean("personDao");
    	PersonDao dao = (PersonDao) context.getBean("personDaoImpl");
    	
    
    	
        Person peter = new Person("Peter", "Sagan");
        Person nasta = new Person("Nasta", "Kuzminova");
       
        
        //save
        dao.save(peter);
        dao.save(nasta);
        
        //getAll
        List<Person> persons = dao.getAll();
        
        
        imprimirListado(persons);
        
        //
        Person p = dao.get(1L);
        System.out.println("-----------"+ p +"-------------");
        
        
        
        //delete
        dao.delete(p);  
//        dao.getEntityManager().remove(dao.get(1L));
        persons = dao.getAll();
        imprimirListado(persons);
       
        
//        context = null;
        
//        ClassPathXmlApplicationContext context2 = new  
//    			  ClassPathXmlApplicationContext("applicationContext.xml");
//      	PersonDao dao2 = (PersonDao) context2.getBean("personDao");
//      	
//      	 List<Person> persons2 = dao2.getAll();
//           
//         System.out.println("----1-----");
//         imprimirListado(persons2);
        
//        eliminar();
        
        context.close();
    }

    private static void eliminar() {
    	ClassPathXmlApplicationContext context = new  
  			  ClassPathXmlApplicationContext("applicationContext.xml");
    	PersonDao dao = (PersonDao) context.getBean("personDao");
    	
    	 List<Person> persons = dao.getAll();
         
         System.out.println("----1-----");
         imprimirListado(persons);
    	
//		Person peter = dao.get(1L);
//		
//		dao.delete(peter);
//		
//		 persons = dao.getAll();
//	        
//		 System.out.println("----2-----");
//	      imprimirListado(persons);
		
		
		
	}
    
    
	private static void imprimirListado(List<Person> persons) {
		for (Person person : persons) {
			System.out.println("----------" + person + "------------");
		}
	}
}
